package com.xu.controller;

import com.github.pagehelper.PageInfo;
import com.xu.pojo.College;
import com.xu.pojo.User;
import com.xu.service.*;
import com.xu.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.Date;
import java.util.List;

@Controller
public class AdminController {
     @Autowired
     @Qualifier("userServiceImpl")
     private UserService userService;

     @Autowired
     @Qualifier("collegeServiceImpl")
     private CollegeService collegeService;

     @Autowired
     @Qualifier("courseServiceImpl")
     private CourseService courseService;

     @Autowired
     @Qualifier("selectServiceImpl")
     private SelectService selectService;
     
     private final int pageSize = 5;




     @RequestMapping("/admin/queryStudent")
     public String queryStudent(Model model){
          PageInfo<User> students = userService.queryUserByRolePaging("student", 1, pageSize);
          List<College> colleges = collegeService.queryAllCollegeExceptOffice();
          model.addAttribute("students",students);
          model.addAttribute("colleges",colleges);
          return "admin/student";
     }

     @RequestMapping("/admin/queryTeacher")
     public String queryTeacher(Model model){
          PageInfo<User> teachers = userService.queryUserByRolePaging("teacher", 1, pageSize);
          List<College> colleges = collegeService.queryAllCollegeExceptOffice();
          model.addAttribute("teachers",teachers);
          model.addAttribute("colleges",colleges);
          return "admin/teacher";
     }

     @RequestMapping("/admin/queryCollege")
     public String queryCollege(Model model){
          PageInfo<College> colleges = collegeService.queryAllCollegePaging(1,pageSize);
          model.addAttribute("colleges",colleges);
          return "admin/college";
     }

     @PostMapping("/admin/queryCollegePaging")
     @ResponseBody
     public PageInfo<College> queryCollegePaging(int pageNum){
          PageInfo<College> colleges = collegeService.queryAllCollegePaging(pageNum,pageSize);
          return colleges;
     }


     @DeleteMapping("/admin/deleteUser")
     @ResponseBody
     public String deleteUser(int userId){
          userService.delteUser(userId);
          return "success";

     }

     @RequestMapping("/admin/update/{userId}")
     public String toUpdate(@PathVariable("userId") int userid, Model model){
          User user = userService.queryUserByID(userid);
          List<College> colleges = collegeService.queryAllCollegeExceptOffice();
          model.addAttribute("user",user);
          model.addAttribute("colleges",colleges);
          return "admin/updateUser";
     }

     @PostMapping("/admin/update")
     public String updateUser(User user){
          //System.out.println(user);
          userService.updateUser(user);
          if (user.getRoles().equals("student"))
               return "redirect:/admin/queryStudent";
          else
               return "redirect:/admin/queryTeacher";
     }

     @RequestMapping("/admin/tomutiadd/{roles}")
     public String tomutiadd(@PathVariable("roles") String roles, Model model){
          model.addAttribute("roles",roles);
          return "admin/mutiAdd";
     }

     @PostMapping("/admin/mutiAdd")
     public String mutiadd(@RequestParam("excelFile") MultipartFile file, String roles, Model model){
          if(file.isEmpty()){
               model.addAttribute("result", "文件为空");
               return "admin/mutiAdd";
          }
          String result = userService.addMutiUser(file, roles);
          model.addAttribute("result", result);
          model.addAttribute("roles",roles);
          return "admin/mutiAdd";
     }

     @RequestMapping("/admin/add/{roles}")
     public String toAdd(@PathVariable("roles") String roles, Model model){
          List<College> colleges = collegeService.queryAllCollegeExceptOffice();
          model.addAttribute("colleges",colleges);
          model.addAttribute("roles",roles);
          return "admin/addUser";
     }

     @PostMapping("/admin/add")
     public String addUser(User user){
          //System.out.println(user);
          userService.addUser(user);
          if (user.getRoles().equals("student"))
               return "redirect:/admin/queryStudent";
          else
               return "redirect:/admin/queryTeacher";
     }

     @PostMapping("/admin/queryUserIf/{roles}")
     @ResponseBody
     public PageInfo<User> queryUserIf(@PathVariable("roles") String roles,String queryCondition,int collegeId,int pageNum){
          PageInfo<User> users = userService.queryUserIfPaging(roles, queryCondition, collegeId, pageNum, pageSize);
          return users;
     }


     @RequestMapping("/admin/selectManage")
     public String toSelectManage(Model model){
          long[] SelectTime = selectService.getSeSelectTime();
          if (SelectTime!=null){
               String startTime = DateUtil.stampToDate(SelectTime[0]);
               String endTime = DateUtil.stampToDate(SelectTime[1]);
               model.addAttribute("startTime",startTime);
               model.addAttribute("endTime",endTime);
          }
          return "admin/selectManage";
     }

     @PostMapping ("/admin/selectManage")
     @ResponseBody
     public String SelectManage(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime){
          return selectService.setSelectTime(startTime, endTime);
     }





}
